package com.albert.funny;



import com.albert.funny.CurrentLocationManager.CurrentLocationListener;


import android.app.Service;
import android.content.Intent;
import android.location.Location;
import android.os.IBinder;
import android.os.RemoteCallbackList;
import android.os.RemoteException;
import android.text.format.DateUtils;

public class CurrentLocationTrackingService extends Service implements CurrentLocationListener
{
    protected boolean isStartRecording;
    protected CurrentLocationManager currentLocManager;
    final RemoteCallbackList<IRemoteLocationTrackServiceCallback> mCallbacks = new RemoteCallbackList<IRemoteLocationTrackServiceCallback>();
    private long lastTimeStamp;

    public void onCreate()
    {
        super.onCreate();
        System.out.println("Create new Service......");
        if(this.getApplication() instanceof LocationTrackApplication)
        {
            LocationTrackApplication application = (LocationTrackApplication)this.getApplication();
            currentLocManager = application.getCurrentLocationManager();
        }
        
        currentLocManager.setUpdateListener(this);

    }
   
    
    @Override
    public IBinder onBind(Intent intent)
    {
        return mBinder;
    }
    @Override
    public void updateCurrentLocation(int conditionflag)
    {
        Location location = this.currentLocManager.getLocation();
        final int N = mCallbacks.beginBroadcast();
        for (int i = 0; i < N; i++)
        {
            try
            {
                mCallbacks.getBroadcastItem(i).locationChanged(conditionflag, location);
            } catch (RemoteException e)
            {
                // The RemoteCallbackList will take care of removing
                // the dead object for us.
            }
        }
        mCallbacks.finishBroadcast();
        logLocation(location, "N/A", false);
       
    }
    
    protected synchronized void logLocation(Location location, String placeName, boolean isForcedLocation)
    {
        if (location != null)
        {
            long duration = System.currentTimeMillis() - this.lastTimeStamp;
            if (duration > 60000 || isForcedLocation)
            {
                LocationRecordManager locationRecordMgr = new LocationRecordManager(this.getApplicationContext());
                locationRecordMgr.execute(
                        "PlaceName:"
                                + placeName
                                + " Time:"
                                + String.valueOf(DateUtils.formatDateTime(this.getApplicationContext(), System.currentTimeMillis(), DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_TIME)), 
                        " GPSProvider:" + String.valueOf(location.getProvider()),
                        " Latitude:" + String.valueOf(location.getLatitude()),
                        " Longtitude:" + String.valueOf(location.getLongitude()),
                        " Altitude:" + String.valueOf(location.getAltitude()),
                        " Accuracy:" + String.valueOf(location.getAccuracy()),
                        " SPEED:" + String.valueOf(location.getSpeed()) + "\n");
                if (!isForcedLocation)
                {
                    this.lastTimeStamp = System.currentTimeMillis();
                }
            }
        }
    }
    
    
    
    protected void startTrackingService()
    {
//        if (!isStartRecording)
        {
            currentLocManager.startUpdateLocation();
            isStartRecording = true;
        }
    }

    protected void stopTrackingService()
    {
//        if(isStartRecording)
        {
            currentLocManager.stopUpdateLocation();
            this.stopSelf();
            isStartRecording = false;
        }
    }
    
    private final IRemoteLocationTrackService.Stub mBinder = new IRemoteLocationTrackService.Stub() {
        public void registerCallback(IRemoteLocationTrackServiceCallback cb) {
            if (cb != null) mCallbacks.register(cb);
        }
        public void unregisterCallback(IRemoteLocationTrackServiceCallback cb) {
            if (cb != null) mCallbacks.unregister(cb);
        }
        @Override
        public void startTracking() throws RemoteException
        {
            startTrackingService();
            
        }
        @Override
        public void stopTracking() throws RemoteException
        {
            // TODO Auto-generated method stub
            stopTrackingService();
        }
        @Override
        public void recordLocation(String placeName) throws RemoteException
        {
            logLocation(currentLocManager.getLocation(), placeName, true);
        }
    };
}
